package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01QryFinanceRecInfo extends CMB01Base implements IFinanceRecInfo {
    @Override
    public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo recInfo) throws Exception {
        RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(reqHeader);
        retAts.writeStart();
        String tmpRespCode ="";
        String tmpRespInfo ="";
        try {
            CMB01Header header =  new CMB01Header();
            header.FUNNAM = "TDAFDCQY";
            XmlTextWriter xmlWriter =  new XmlTextWriter();
            writeCommonHeader(xmlWriter,header);
//            输入	TDAFDCQYX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFDCQYX");
//            直联企业系统客户号	COREID	C (10)	否
            xmlWriter.writeElementString("COREID", bankFront.org);
//            对接企业类型	COTYPE	C (1)	否	1核心企业 2服务商
            xmlWriter.writeElementString("COTYPE", "1");
//            核心企业组织机构代码	SCOCOD	C (32)	可
            xmlWriter.writeElementString("SCOCOD", "");
//            经销商代码	DLENUM	C (40)	可
            xmlWriter.writeElementString("DLENUM", recInfo.DealerCode);
//            经销商组织机构代码	DLECOD	C (32)	可	经销商代码、经销商组织机构代码二者必输其一
            xmlWriter.writeElementString("DLECOD", "");
//            赎证日期起	STADAT	D	可
            xmlWriter.writeElementString("STADAT", recInfo.BeginDate);
//            赎证日期止	ENDDAT	D	可
            xmlWriter.writeElementString("ENDDAT", recInfo.EndDate);
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
                        tmpRespCode ="0";
                        tmpRespInfo = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        while (tmpBkReader.read("/CMBSDKPGK/TDAFDCQYY")){
                            RetAtsFinanceRecInfoDetail detail = new RetAtsFinanceRecInfoDetail();
//                            经销商名称	DLENAM	Z(100)	否	经销商名称
                            detail.DealerCode =tmpBkReader.readedXElement.getSingleText("DLENAM");
//                            底盘号/车架号	CHANUM	C(40)	否
                            detail.VIN =tmpBkReader.readedXElement.getSingleText("CHANUM");
//                            赎证时间	DECDAT	C(20)	可	需要精确到时分秒 格式：YYYY-MM-DD HH:MM:SS (中间空格分隔)
                            detail.RepaymentDate =tmpBkReader.readedXElement.getSingleText("DECDAT");
//                            入库日期	ENTDAT	C(20)	否	需要精确到时分秒 格式：YYYY-MM-DD HH:MM:SS (中间空格分隔)
                            detail.CarMortgageTime =tmpBkReader.readedXElement.getSingleText("ENTDAT");
                            retAts.writeRetDetail(detail);
                        }
                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            tmpRespCode ="-1";
            tmpRespInfo = ex.getMessage();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
